package es.mgil.energyToggle;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.SettingNotFoundException;
import android.provider.Settings.System;
import android.view.WindowManager;

public class EnergyToggle extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		toggleBrightness();
	}

	private void toggleBrightness() {
		// if (PreferencesToggleWidgetProvider.STATE ==
		// PreferencesToggleWidgetProvider.MIN_STATE) {
		// SharedPreferences prefs = PreferenceManager
		// .getDefaultSharedPreferences(getApplicationContext());
		//
		// System.putInt(
		// getContentResolver(),
		// System.SCREEN_BRIGHTNESS,
		// prefs.getInt(
		// PreferencesToggleWidgetProvider.normal_brightness_ID,
		// 1));
		// } else {
		// System.putInt(getContentResolver(), System.SCREEN_BRIGHTNESS,
		// PreferencesToggleWidgetProvider.min_brightness);
		// }

		if (EnergyToggleWidgetProvider.changing_brightness) {
			EnergyToggleWidgetProvider.changing_brightness = false;
			int brightness = -1;
			try {
				brightness = System.getInt(getContentResolver(),
						System.SCREEN_BRIGHTNESS);
			} catch (SettingNotFoundException e) {
			}

			WindowManager.LayoutParams lp = getWindow().getAttributes();
			lp.screenBrightness = brightness;
			getWindow().setAttributes(lp);

			Thread t = new Thread() {
				public void run() {
					try {
						sleep(300);
					} catch (InterruptedException e) {
					}
					EnergyToggle.this.finish();
				}
			};
			t.start();
		}
	}
}
